package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbAdmin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("login")
public class LoginController {

    @RequestMapping("getLogin")
    @ResponseBody
    public AjaxResult getLogin(TbAdmin admin){
        Logger logger = LoggerFactory.getLogger(LoginController.class);
        /*通过shrio认证登录*/
        Subject subject = SecurityUtils.getSubject();
        /*封装用户数据*/
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getAdminName(),admin.getAdminPwd());
        try {
            subject.login(token);
            /*登陆成功跳转首页*/
            return AjaxResult.R(ResultEnum.SUCCESS);
        }catch (UnknownAccountException e){
            /*用户名不存在*/
            System.out.println("用户名不存在");
            logger.error("登录:用户名不存在!错误内容:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            logger.error("登录:密码错误!错误内容:{}",e);
            return AjaxResult.ERROR();
        }
    }

}
